import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  UseInterceptors,
  ClassSerializerInterceptor,
  Query,
} from '@nestjs/common';
import { RecordCalcService } from './record_calc.service';
import { CreateRecordCalcDto } from './dto/create-record_calc.dto';
// import { UpdateRecordCalcDto } from './dto/update-record_calc.dto';
import { ApiOperation } from '@nestjs/swagger';
import { User } from 'src/auth/decorator/user.decorator';
import { UserEntity } from 'src/user/entities/user.entity';
import { ListRecordCalcDto } from './dto/list-record_calc.dto';
import { BusinessException } from 'src/common/filter/business-exception/business.exception';

@Controller('record-calc')
export class RecordCalcController {
  constructor(private readonly recordCalcService: RecordCalcService) {}

  @Post()
  @ApiOperation({ summary: '添加记录' })
  @UseInterceptors(ClassSerializerInterceptor)
  create(
    @Body() createRecordCalcDto: CreateRecordCalcDto,
    @User() user: UserEntity,
  ) {
    if (typeof createRecordCalcDto.company_id !== 'string')
      throw new BusinessException('公司参数错误');
    return this.recordCalcService.create(createRecordCalcDto, user);
  }

  @Get()
  @UseInterceptors(ClassSerializerInterceptor)
  @ApiOperation({ summary: '按条件获取记录' })
  findAll(@Query() searchParam: ListRecordCalcDto, @User() user: UserEntity) {
    if (!searchParam.current) searchParam.current = 1;
    if (!searchParam.size) searchParam.size = 20;
    // 搜索公司 / 备注
    return this.recordCalcService.findAll(searchParam, user);
  }

  // @Get(':id')
  // findOne(@Param('id') id: string) {
  //   return this.recordCalcService.findOne(+id);
  // }

  // @Patch(':id')
  // @ApiOperation({ summary: '修改记录' })
  // update(
  //   @Param('id') id: string,
  //   @Body() updateRecordCalcDto: UpdateRecordCalcDto,
  // ) {
  //   return this.recordCalcService.update(+id, updateRecordCalcDto);
  // }

  @Delete(':id')
  @ApiOperation({ summary: '删除记录' })
  remove(@Param('id') id: string, @User() user: UserEntity) {
    return this.recordCalcService.remove(id, user);
  }
}
